package com.karl.mytask_2.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.karl.mytask_2.R;
import com.karl.mytask_2.activity.PublishActivity;
import com.karl.mytask_2.activity.SettingsActivity;
import com.karl.mytask_2.activity.PersonalInfoActivity;
import com.karl.mytask_2.adapter.UserAdapter;
import com.karl.mytask_2.bean.MyUser;

import org.xutils.image.ImageOptions;
import org.xutils.x;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.listener.GetListener;

/**
 * Created by Karl on 2016/5/20.
 */
public class UserFragment extends Fragment implements View.OnClickListener {
    private View mUserView;
    private RecyclerView mRecyclerView;
    private UserAdapter mAdapter;
    //登陆后选项列表
    private String[] mListTitle = new String[] {"基本信息","所有任务","邀请好友","设置"};
    private int[] mListImg =
            new int[]{R.mipmap.user_info,
                    R.mipmap.user_all,
                    R.mipmap.user_share,
                    R.mipmap.user_settings};

    //ToolBar
    private TextView mToolBarTxt,mToolBarBtn;
    private ImageView mToolBarImg;
    private ImageOptions mImageOptions2 = new ImageOptions.Builder().setAutoRotate(true).setCircular(true)
            .setFadeIn(true).setSize(80,80).setLoadingDrawableId(R.mipmap.ic_launcher).build();
    public String  mIconUrl = "http://cdn.duitang.com/uploads/blog/201412/07/20141207010951_Ut5zA.thumb.224_0.jpeg";

    //用户头像
    private ImageView mUserIcon;

    //添加图形配置
    ImageOptions mImageOptions = new ImageOptions.Builder().setAutoRotate(true).setCircular(true)
            .setFadeIn(true).setSize(80,80).setLoadingDrawableId(R.mipmap.ic_launcher).build();

    //用户信息
    private TextView mUserName,mUserSignature;



    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mUserView = inflater.inflate(R.layout.fragment_user,container,false);
        x.Ext.init(getActivity().getApplication());
        initView();
        initEvent();
        return mUserView;
    }
    private void initView(){
        mRecyclerView = (RecyclerView) mUserView.findViewById(R.id.user_recycler_view);
        mToolBarTxt = (TextView) mUserView.findViewById(R.id.tool_bar_txt);
        mToolBarBtn = (TextView)mUserView.findViewById(R.id.tool_bar_btn);
        mUserIcon = (ImageView) mUserView.findViewById(R.id.user_icon);
        mUserName = (TextView) mUserView.findViewById(R.id.user_name);
        mUserSignature = (TextView) mUserView.findViewById(R.id.user_signature);
        mToolBarImg = (ImageView) mUserView.findViewById(R.id.app_img);


    }
    private void initEvent(){
        //RecyclerView设置
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        mRecyclerView.setHasFixedSize(true);
        mAdapter = new UserAdapter(mListTitle,mListImg);
        mRecyclerView.setAdapter(mAdapter);
        //创建任务的监听事件
        mToolBarBtn.setOnClickListener(this);
        //RecyclerView点击事件
        mAdapter.setOnItemClickListener(new UserAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, String data) {
                switch (data) {
                    case "基本信息":
                        startActivity(new Intent().setClass(getActivity(), PersonalInfoActivity.class));
                        break;
                    case "所有任务":
                        Toast.makeText(getContext(), "待实现", Toast.LENGTH_SHORT).show();
                        break;
                    case "邀请好友":
                        Toast.makeText(getContext(), "待实现", Toast.LENGTH_SHORT).show();
                        break;
                    case "设置":
                        startActivity(new Intent().setClass(getContext(), SettingsActivity.class));
                        break;
                }

            }
        });
        //toolBar事件
        mToolBarTxt.setText("个人中心");
        x.image().bind(mToolBarImg,mIconUrl,mImageOptions2);


        getUserInfo();

    }


    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.tool_bar_btn:
                startActivity(new Intent().setClass(getContext(), PublishActivity.class));
                break;

        }
    }

    private void getUserInfo(){

        String id = BmobUser.getCurrentUser(getContext()).getObjectId();
        BmobQuery<MyUser> queryUserInfo = new BmobQuery<>();
        queryUserInfo.getObject(getContext(), id, new GetListener<MyUser>() {
            @Override
            public void onSuccess(MyUser myUser) {
                mUserName.setText(myUser.getNickName());
                if (myUser.getSignature()== null){
                    mUserSignature.setText("你还什么都没有留下哦");
                }else {
                    mUserSignature.setText(myUser.getSignature());
                }
                if (myUser.getIcon() == null){
                    //圆形图标配置
                    final String url="http://cdnq.duitang.com/uploads/item/201412/27/20141227140012_BV2Bu.jpeg";
                    x.image().bind(mUserIcon, url, mImageOptions);
                }else {
                    x.image().bind(mUserIcon,myUser.getIcon(),mImageOptions);
                }
            }

            @Override
            public void onFailure(int i, String s) {
                Toast.makeText(getActivity(), "更新用户信息失败，请检查网络设置或者重新启动~", Toast.LENGTH_SHORT).show();
            }
        });

    }
}
